package edu.oregonstate.vision.common;

import static edu.oregonstate.vision.common.ImageUtility.convertToGrayScale;
import static edu.oregonstate.vision.common.ImageUtility.getImage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import Jama.Matrix;

public class UniformPointSelector extends PointSelector {

	private static PointSelector pointSelector;

	public static List<Point> findNearbyPoints(List<Point> points, Point point,
			int neighborhoodSize) {
		List<Point> nearbyPoints = new ArrayList<Point>();
		Matrix distanceMatrix = EuclideanDistanceCalculator.getInstance()
				.calculate(points, points);
		for (int i = 0; i < points.size(); i++) {

		}
		return nearbyPoints;
	}

	@Override
	public List<Point> performSampling(File file) {

		List<Point> points = new ArrayList<Point>();
		BufferedImage bufferedImage = convertToGrayScale(getImage(file))
				.getAsBufferedImage();
		int index = 0;
		for (int i = 0; i < bufferedImage.getWidth(); i++) {
			for (int j = 0; j < bufferedImage.getHeight(); j++) {
				if ((bufferedImage.getRGB(i, j) & 0xff) != 0) {
					index++;
				}
			}
		}
		Random random = new Random();
		BufferedImage outputImage = new BufferedImage(bufferedImage.getWidth(),
				bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);

		index = 0;
		for (int i = 0; i < bufferedImage.getWidth(); i++) {
			for (int j = 0; j < bufferedImage.getHeight(); j++) {
				if ((bufferedImage.getRGB(i, j) & 0xff) != 0) {
					if (index % 10 == 0) {
						outputImage.setRGB(i, j, 255 * 0x00010101);
						points.add(new Point(i, j));
					} else {
						outputImage.setRGB(i, j, 0 * 0x00010101);
					}
					index++;
				}
			}
		}

		ImageUtility.showImage(outputImage);

		/**
		 * Uniform with probability.
		 */
		// for (int i = 0; i < bufferedImage.getWidth(); i++) {
		// for (int j = 0; j < bufferedImage.getHeight(); j++) {
		// if ((bufferedImage.getRGB(i, j) & 0xff) != 0) {
		// if (random.nextFloat() < 0.1f) {
		// outputImage.setRGB(i, j, 255 * 0x00010101);
		// } else {
		// outputImage.setRGB(i, j, 0 * 0x00010101);
		// }
		// }
		// }
		// }
		// ImageUtility.showImage(outputImage);
		// TODO Auto-generated method stub
		return points;
	}

	public static PointSelector getInstance() {
		if (pointSelector == null)
			pointSelector = new UniformPointSelector();
		return pointSelector;
	}
}
